<template>
  <!-- 商品详情 -->
  <div class="product-detail">
    <!-- 公用头部组件 -->
    <custom-header :title="'商品详情'"></custom-header>
    <div class="detail-content">
      <div class="detail-swipe-wrap">
        <!-- 轮播图组件 -->
        <van-swipe class="my-swipe" indicator-color="#1baeae">
          <van-swipe-item
            v-for="(item, index) in detail.goodsCarouselList"
            :key="index"
          >
            <img :src="item" alt="" />
          </van-swipe-item>
        </van-swipe>
      </div>
      <!-- 商品信息描述 -->
      <div class="product-info">
        <div class="product-title">{{ detail.goodsName || "" }}</div>
        <div class="product-desc">免邮费 顺丰快递</div>
        <div class="product-price">
          <span>¥{{ detail.sellingPrice || "" }}</span>
          <!-- <span>库存203</span> -->
        </div>
      </div>
      <!-- 商品参数 -->
      <div class="product-intro">
        <ul>
          <li>概述</li>
          <li>参数</li>
          <li>安装服务</li>
          <li>常见问题</li>
        </ul>
        <!-- 详情页面 -->
        <div
          class="product-content"
          v-html="detail.goodsDetailContent || ''"
        ></div>
      </div>
    </div>
    <!-- 底部操作按钮 -->
    <van-action-bar>
      <van-action-bar-icon icon="chat-o" text="客服" />
      <van-action-bar-icon
        icon="cart-o"
        :badge="!count ? '' : count"
        text="购物车"
        @click="goTo"
      />
      <van-action-bar-button
        type="warning"
        text="加入购物车"
        @click="handleAddCart"
      />
      <van-action-bar-button type="danger" text="立即购买" @click="handleBuy" />
    </van-action-bar>
  </div>
</template>
  
<script>
import { reactive, onMounted, toRefs, getCurrentInstance, computed } from "vue";
import CustomHeader from "components/CustomHeader.vue";
import { getDetail } from "service/goods";
import { useRoute } from "vue-router";
import { addCart } from "service/cart";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { Toast } from "vant";

export default {
  name: "ProductDetail",
  components: {
    CustomHeader,
  },
  setup() {
    const {
      appContext: {
        config: { globalProperties },
      },
    } = getCurrentInstance();
    const route = useRoute(); //生成路由实例
    const state = reactive({
      detail: {
        goodsCarouselList: [], // 声明轮播图数组
      },
    });

    onMounted(() => {
      initData();
    });

    const router = useRouter();
    const store = useStore(); //生成store实例

    const initData = async () => {
      const { id } = route.params;
      const { data } = await getDetail(id);
      data.goodsCarouselList = data.goodsCarouselList.map((i) =>
        globalProperties.$filters.prefix(i)
      );
      state.detail = data;
      // 更新购物车商品数量
      store.dispatch("updateCart");
    };
    // 前往购物车页面
    const goTo = () => {
      router.push({ path: "/cart" });
    };
    // 添加购物车
    const handleAddCart = async () => {
      const { resultCode } = await addCart({
        goodsCount: 1,
        goodsId: state.detail.goodsId,
      });
      if (resultCode == 200) Toast.success("添加成功");
      // 更新购物车中的商品数量
      store.dispatch("updateCart");
    };
    // 立即购买
    const handleBuy = async () => {
      await addCart ({ goodsCount: 1, goodsId: state.detail.goodsId })
      // 立即调用购买结构，更新购物车中的商品数量
      store.dispatch("updateCart");
      router.push({ path: "/cart" });
    }

    // 通过计算方法 computed，监听store.state.cartCount 购物车商品数量变化
    const count = computed(() => {
      console.log('cartCount', store.state.cartCount)
      return store.state.cartCount
    })
    return {
      ...toRefs(state),
      goTo,
      handleAddCart,
      handleBuy,
      count
    };
  },
};
</script>
  
  <style lang="less">
@import "../theme/custom.less";
.product-detail {
  .detail-header {
    .fj();
    .wh(100%, 44px);
    line-height: 44px;
    padding: 0 10px;
    .boxSizing();
    color: #252525;
    background: #fff;
    border-bottom: 1px solid #dcdcdc;
    .product-name {
      font-size: 14px;
    }
  }
  .detail-content {
    height: calc(100vh - 50px);
    overflow: hidden;
    overflow-y: auto;
    .detail-swipe-wrap {
      .my-swipe .van-swipe-item {
        img {
          width: 100%;
          // height: 300px;
        }
      }
    }
    .product-info {
      padding: 0 10px;
      .product-title {
        font-size: 18px;
        text-align: left;
        color: #333;
      }
      .product-desc {
        font-size: 14px;
        text-align: left;
        color: #999;
        padding: 5px 0;
      }
      .product-price {
        .fj();
        span:nth-child(1) {
          color: #f63515;
          font-size: 22px;
        }
        span:nth-child(2) {
          color: #999;
          font-size: 16px;
        }
      }
    }
    .product-intro {
      width: 100%;
      padding-bottom: 50px;
      ul {
        .fj();
        width: 100%;
        margin: 10px 0;
        li {
          flex: 1;
          padding: 5px 0;
          text-align: center;
          font-size: 15px;
          border-right: 1px solid #999;
          box-sizing: border-box;
          &:last-child {
            border-right: none;
          }
        }
      }
      .product-content {
        padding: 0 20px;
        img {
          width: 100%;
        }
      }
    }
  }
  .van-action-bar-button--warning {
    background: linear-gradient(to right, #6bd8d8, @primary);
  }
  .van-action-bar-button--danger {
    background: linear-gradient(to right, #0dc3c3, #098888);
  }
}
</style>